package com.project.service.impl;

import com.project.bean.PurchaseBean;
import com.project.bean.PurchaseCarBean;
import com.project.bean.SupplierBean;
import com.project.bean.UserBean;
import com.project.dao.IPurchaseCarDao;
import com.project.dao.IPurchaseDao;
import com.project.dao.IPurchaseInfoDao;
import com.project.service.IProductService;
import com.project.service.IPurchaseCarService;
import com.project.util.InfoResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * @author lyl
 * description:
 */
@Service
@Transactional
public class PurchaseCarServiceImpl implements IPurchaseCarService {
    @Autowired
    private IPurchaseCarDao iPurchaseCarDao;
    @Autowired
    private IPurchaseInfoDao iPurchaseInfo;
    @Autowired
    private IPurchaseDao iPurchaseDao;
    @Autowired
    private IProductService iProductService;

    @Override
    public List<PurchaseCarBean> showPurchaseCarById(int supplierId, int userId) {
        return iPurchaseCarDao.showPurchaseCarById(supplierId, userId);
    }

    @Override
    public InfoResult updatePurchaseNum(int supplierId, int userId, int productId, int num) {
        int x = iPurchaseCarDao.updatePurchaseNum(supplierId, userId, productId, num);
        if (x == 1) {
            return new InfoResult(0, "更新数量成功");
        }
        return new InfoResult(1, "更新数量失败");
    }

    @Override
    public int addPurchaseToCar(int userId, int supplierId, long productId, int purchaseNum) {
        return iPurchaseCarDao.addNewPurchaseToCar(userId, supplierId, productId, purchaseNum);
    }

    @Override
    public InfoResult purchaseProducts(int supplierId, int userId) {
        //现有购物车的集合
        List<PurchaseCarBean> list = iPurchaseCarDao.showNumOverZero(userId, supplierId);

        //创建实体入库单
        PurchaseBean p = new PurchaseBean();
        p.setPurchaseDate(LocalDate.now());
        UserBean u = new UserBean();
        u.setUserId(userId);
        SupplierBean su = new SupplierBean();
        su.setSupplierId(supplierId);
        p.setSupplierBean(su);
        p.setUserBean(u);
        int x1 = iPurchaseDao.addPurchase(p);
        int x2 = 0;

        //循环添加清单详情
        for (int i = 0; i < list.size(); i++) {
            //添加明细
            iPurchaseInfo.addPurchaseInfo(list.get(i).getProductBean().getProductId(), list.get(i).getPurchaseNum(), list.get(i).getProductBean().getSalePrice(), p.getPurchaseId());
            //更新库存货物数量
            iProductService.updateProductNum(list.get(i).getProductBean().getProductId(), list.get(i).getPurchaseNum(), true);
            x2++;
        }
        //清空预处理入库单的商品数量
        int x = iPurchaseCarDao.cleanNum(userId, supplierId);
        if (x > 0) {
            return new InfoResult(0, "入库采购成功,清空了" + x + "项商品的采购数量,创建了" + x1 + "个新订单,新增" + x2 + "条订单详情");
        }
        return new InfoResult(1, "入库失败");
    }
}
